from ctypes import *
from ctypes.wintypes import *

_imagehlp = WinDLL("imagehlp")
_dbhelp = WinDLL("dbghelp")

def nonnull(result, func, args):
    if result:
        return result
    raise WinError()

BOOL_errcheck = nonnull

################################################################
##if __debug__:
##    from ctypeslib.dynamic_module import include
##    include("""\
##    #define UNICODE
##    #define NO_STRICT
##    #define WINVER 0x501
##    #define _WIN32_WINNT 0x501
##    #include <windows.h>
##    #include <imagehlp.h>
##    """,
##            persist=True)
################################################################
# Everyting below this line is autogenerated.
STRING = c_char_p
class _LOADED_IMAGE(Structure):
    pass
LOADED_IMAGE = _LOADED_IMAGE
CHAR = c_char
PSTR = STRING
UCHAR = c_ubyte
PUCHAR = POINTER(UCHAR)
class _IMAGE_NT_HEADERS(Structure):
    pass
PIMAGE_NT_HEADERS32 = POINTER(_IMAGE_NT_HEADERS)
class _IMAGE_SECTION_HEADER(Structure):
    pass
PIMAGE_SECTION_HEADER = POINTER(_IMAGE_SECTION_HEADER)
boolean = c_ubyte
class _LIST_ENTRY(Structure):
    pass
_LIST_ENTRY._fields_ = [
    ('Flink', POINTER(_LIST_ENTRY)),
    ('Blink', POINTER(_LIST_ENTRY)),
]
LIST_ENTRY = _LIST_ENTRY
_LOADED_IMAGE._fields_ = [
    ('ModuleName', PSTR),
    ('hFile', HANDLE),
    ('MappedAddress', PUCHAR),
    ('FileHeader', PIMAGE_NT_HEADERS32),
    ('LastRvaSection', PIMAGE_SECTION_HEADER),
    ('NumberOfSections', DWORD),
    ('Sections', PIMAGE_SECTION_HEADER),
    ('Characteristics', DWORD),
    ('fSystemImage', boolean),
    ('fDOSImage', boolean),
    ('Links', LIST_ENTRY),
    ('SizeOfImage', DWORD),
]
class _IMAGE_FILE_HEADER(Structure):
    pass
_IMAGE_FILE_HEADER._fields_ = [
    ('Machine', WORD),
    ('NumberOfSections', WORD),
    ('TimeDateStamp', DWORD),
    ('PointerToSymbolTable', DWORD),
    ('NumberOfSymbols', DWORD),
    ('SizeOfOptionalHeader', WORD),
    ('Characteristics', WORD),
]
IMAGE_FILE_HEADER = _IMAGE_FILE_HEADER
class _IMAGE_OPTIONAL_HEADER(Structure):
    pass
class _IMAGE_DATA_DIRECTORY(Structure):
    pass
_IMAGE_DATA_DIRECTORY._fields_ = [
    ('VirtualAddress', DWORD),
    ('Size', DWORD),
]
IMAGE_DATA_DIRECTORY = _IMAGE_DATA_DIRECTORY
_IMAGE_OPTIONAL_HEADER._fields_ = [
    ('Magic', WORD),
    ('MajorLinkerVersion', BYTE),
    ('MinorLinkerVersion', BYTE),
    ('SizeOfCode', DWORD),
    ('SizeOfInitializedData', DWORD),
    ('SizeOfUninitializedData', DWORD),
    ('AddressOfEntryPoint', DWORD),
    ('BaseOfCode', DWORD),
    ('BaseOfData', DWORD),
    ('ImageBase', DWORD),
    ('SectionAlignment', DWORD),
    ('FileAlignment', DWORD),
    ('MajorOperatingSystemVersion', WORD),
    ('MinorOperatingSystemVersion', WORD),
    ('MajorImageVersion', WORD),
    ('MinorImageVersion', WORD),
    ('MajorSubsystemVersion', WORD),
    ('MinorSubsystemVersion', WORD),
    ('Win32VersionValue', DWORD),
    ('SizeOfImage', DWORD),
    ('SizeOfHeaders', DWORD),
    ('CheckSum', DWORD),
    ('Subsystem', WORD),
    ('DllCharacteristics', WORD),
    ('SizeOfStackReserve', DWORD),
    ('SizeOfStackCommit', DWORD),
    ('SizeOfHeapReserve', DWORD),
    ('SizeOfHeapCommit', DWORD),
    ('LoaderFlags', DWORD),
    ('NumberOfRvaAndSizes', DWORD),
    ('DataDirectory', IMAGE_DATA_DIRECTORY * 16),
]
IMAGE_OPTIONAL_HEADER32 = _IMAGE_OPTIONAL_HEADER
_IMAGE_NT_HEADERS._fields_ = [
    ('Signature', DWORD),
    ('FileHeader', IMAGE_FILE_HEADER),
    ('OptionalHeader', IMAGE_OPTIONAL_HEADER32),
]
class N21_IMAGE_SECTION_HEADER4DOLLAR_23E(Union):
    pass
N21_IMAGE_SECTION_HEADER4DOLLAR_23E._fields_ = [
    ('PhysicalAddress', DWORD),
    ('VirtualSize', DWORD),
]
_IMAGE_SECTION_HEADER._fields_ = [
    ('Name', BYTE * 8),
    ('Misc', N21_IMAGE_SECTION_HEADER4DOLLAR_23E),
    ('VirtualAddress', DWORD),
    ('SizeOfRawData', DWORD),
    ('PointerToRawData', DWORD),
    ('PointerToRelocations', DWORD),
    ('PointerToLinenumbers', DWORD),
    ('NumberOfRelocations', WORD),
    ('NumberOfLinenumbers', WORD),
    ('Characteristics', DWORD),
]
PLOADED_IMAGE = POINTER(_LOADED_IMAGE)
MapAndLoad = _imagehlp.MapAndLoad
MapAndLoad.restype = BOOL
MapAndLoad.argtypes = [PSTR, PSTR, PLOADED_IMAGE, BOOL, BOOL]
MapAndLoad.errcheck = BOOL_errcheck
PVOID = c_void_p
USHORT = c_ushort
PULONG = POINTER(ULONG)
ImageDirectoryEntryToData = _imagehlp.ImageDirectoryEntryToData
ImageDirectoryEntryToData.restype = PVOID
ImageDirectoryEntryToData.argtypes = [PVOID, boolean, USHORT, PULONG]
IMAGE_DIRECTORY_ENTRY_EXPORT = 0 # Variable c_int
class _IMAGE_EXPORT_DIRECTORY(Structure):
    pass
IMAGE_EXPORT_DIRECTORY = _IMAGE_EXPORT_DIRECTORY
_IMAGE_EXPORT_DIRECTORY._fields_ = [
    ('Characteristics', DWORD),
    ('TimeDateStamp', DWORD),
    ('MajorVersion', WORD),
    ('MinorVersion', WORD),
    ('Name', DWORD),
    ('Base', DWORD),
    ('NumberOfFunctions', DWORD),
    ('NumberOfNames', DWORD),
    ('AddressOfFunctions', DWORD),
    ('AddressOfNames', DWORD),
    ('AddressOfNameOrdinals', DWORD),
]
PIMAGE_NT_HEADERS = PIMAGE_NT_HEADERS32
ImageNtHeader = _imagehlp.ImageNtHeader
ImageNtHeader.restype = PIMAGE_NT_HEADERS
ImageNtHeader.argtypes = [PVOID]
ImageRvaToVa = _imagehlp.ImageRvaToVa
ImageRvaToVa.restype = PVOID
ImageRvaToVa.argtypes = [PIMAGE_NT_HEADERS, PVOID, DWORD, POINTER(PIMAGE_SECTION_HEADER)]
UnMapAndLoad = _imagehlp.UnMapAndLoad
UnMapAndLoad.restype = BOOL
UnMapAndLoad.argtypes = [PLOADED_IMAGE]
UnMapAndLoad.errcheck = BOOL_errcheck
STRING = c_char_p
PCSTR = STRING
UnDecorateSymbolName = _dbhelp.UnDecorateSymbolName
UnDecorateSymbolName.restype = DWORD
UnDecorateSymbolName.argtypes = [PCSTR, PSTR, DWORD, DWORD]
UNDNAME_COMPLETE = 0 # Variable c_int
UNDNAME_NO_LEADING_UNDERSCORES = 1 # Variable c_int
UNDNAME_NO_MS_KEYWORDS = 2 # Variable c_int
UNDNAME_NO_FUNCTION_RETURNS = 4 # Variable c_int
UNDNAME_NO_ALLOCATION_MODEL = 8 # Variable c_int
UNDNAME_NO_ALLOCATION_LANGUAGE = 16 # Variable c_int
UNDNAME_NO_MS_THISTYPE = 32 # Variable c_int
UNDNAME_NO_CV_THISTYPE = 64 # Variable c_int
UNDNAME_NO_THISTYPE = 96 # Variable c_int
UNDNAME_NO_ACCESS_SPECIFIERS = 128 # Variable c_int
UNDNAME_NO_THROW_SIGNATURES = 256 # Variable c_int
UNDNAME_NO_MEMBER_TYPE = 512 # Variable c_int
UNDNAME_NO_RETURN_UDT_MODEL = 1024 # Variable c_int
UNDNAME_32_BIT_DECODE = 2048 # Variable c_int
UNDNAME_NAME_ONLY = 4096 # Variable c_int
UNDNAME_NO_ARGUMENTS = 8192 # Variable c_int
UNDNAME_NO_SPECIAL_SYMS = 16384 # Variable c_int
